//
//  BMNewDatePickerView.m
//  bigmama
//
//  Created by huyuchen on 2017/8/15.
//  Copyright © 2017年 盛成文化. All rights reserved.
//

#import "BMNewDatePickerView.h"

@interface BMNewDatePickerView ()
{
    NSDate *_selectedDate;
}

@property (strong, nonatomic) BMPickerViewToolBar *toolBar;
@property (strong, nonatomic) UIDatePicker *pickerView;
@property (strong, nonatomic) BMDatePickerViewStyle *style;
@property (copy, nonatomic) DateSelectedBlock valueDidChangeBlock;
@end

@implementation BMNewDatePickerView

- (instancetype)initWithToolBarText:(NSString *)toolBarText pickerViewStyle:(BMDatePickerViewStyle *)style toolBarStyle:(BMPickerViewToolBarStyle)toolBarStyle valueDidChangedBlock:(DateSelectedBlock)valueDidChangedBlock cancelBlock:(BtnClickBlock)cancelBlock doneBlock:(DateClickBlock)doneBlock{
    if (self = [super init]) {
        // 默认为当前时间
        _selectedDate = [NSDate date];
        _style = style;
        _valueDidChangeBlock = valueDidChangedBlock;
        __weak typeof(self) weakSelf = self;
        _toolBar = [[BMPickerViewToolBar alloc] initWithToolbarText:toolBarText style:toolBarStyle cancelBlock:cancelBlock doneBlock:^{
            __strong typeof(weakSelf) strongSelf = weakSelf;
            if (strongSelf) {
                if (doneBlock) {
                    doneBlock(strongSelf->_selectedDate);
                }
            }
        }];
        
        self.backgroundColor = [UIColor whiteColor];
        
        [self addSubview:_toolBar];
        [self addSubview:self.pickerView];
        [self setupUI];
        /// 选中日期改变
        [self setupSelectedValueDidChanged];
    }
    return self;
}

- (void)setupUI{
    CGFloat kToolBarHeight = 44.0f;
    
    [self.toolBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self);
        make.height.mas_equalTo(kToolBarHeight);
    }];
    
    [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(260-kToolBarHeight);
    }];
    
//    NSLayoutConstraint *toolBarLeft = [NSLayoutConstraint constraintWithItem:self.toolBar attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0f];
//    NSLayoutConstraint *toolBarRight = [NSLayoutConstraint constraintWithItem:self.toolBar attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0f];
//    
//    NSLayoutConstraint *toolBarHeight = [NSLayoutConstraint constraintWithItem:self.toolBar attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:kToolBarHeight];
//    NSLayoutConstraint *toolBarTop = [NSLayoutConstraint constraintWithItem:self.toolBar attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0f];
//    self.toolBar.translatesAutoresizingMaskIntoConstraints = NO;
//    [self addConstraints:@[toolBarLeft, toolBarRight, toolBarHeight, toolBarTop]];
//    
//    NSLayoutConstraint *pickerViewLeft = [NSLayoutConstraint constraintWithItem:self.pickerView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0f];
//    NSLayoutConstraint *pickerViewRight = [NSLayoutConstraint constraintWithItem:self.pickerView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0f];
//    
//    NSLayoutConstraint *pickerViewHeight = [NSLayoutConstraint constraintWithItem:self.pickerView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:self.bounds.size.height - kToolBarHeight];
//    NSLayoutConstraint *pickerViewBottom = [NSLayoutConstraint constraintWithItem:self.pickerView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0f];
//    self.pickerView.translatesAutoresizingMaskIntoConstraints = NO;
//    [self addConstraints:@[pickerViewLeft, pickerViewRight, pickerViewHeight, pickerViewBottom]];
}

- (void)setupSelectedValueDidChanged {
    if (_valueDidChangeBlock) {
        _valueDidChangeBlock(_selectedDate);
    }
}
- (void)datePickerDidSelected:(UIDatePicker *)datePicker {
    _selectedDate = datePicker.date;
    [self setupSelectedValueDidChanged];
    
}

- (void)layoutSubviews{
    [super layoutSubviews];

}

- (UIDatePicker *)pickerView {
    if (!_pickerView) {
        UIDatePicker *picker = [UIDatePicker new];
        [picker addTarget:self action:@selector(datePickerDidSelected:) forControlEvents:UIControlEventValueChanged];
        picker.datePickerMode = UIDatePickerModeDate;
        picker.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
        if (_style) {
            if (_style.date && [_style.date isKindOfClass:[NSDate class]]) {
                picker.date = _style.date;
            }
            if (_style.minimumDate && [_style.minimumDate isKindOfClass:[NSDate class]]) {
                picker.minimumDate = _style.minimumDate;
            }
            if (_style.maximumDate && [_style.maximumDate isKindOfClass:[NSDate class]]) {
                picker.maximumDate = _style.maximumDate;
            }
            if (_style.locale && [_style.locale isKindOfClass:[NSLocale class]]) {
                picker.locale = _style.locale;
            }
            
            picker.datePickerMode = _style.datePickerMode;
            
            /// 设置默认选中值
            if (_style.date && [_style.date isKindOfClass:[NSDate class]]) {
                _selectedDate = _style.date;
            }
            
        }
        
        _pickerView = picker;
    }
    return _pickerView;
}


@end

